package com.erp.erp_ui.Purchase.Service;

import com.erp.erp_entitys.Purchase.PurchaseFeedback;
import com.erp.erp_entitys.Purchase.QueryPurchaseFeedbackVO;
import com.erp.erp_servers.Purchase.IPurchaseFeedbackService;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author zjk
 * @version 1.0
 * @description
 * @date 2021-07-05 21:56
 */
@Service
public class PurchaseFeedbackAllService {

    private final IPurchaseFeedbackService purchaseFeedbackService;


    public PurchaseFeedbackAllService(IPurchaseFeedbackService purchaseFeedbackService) {
        this.purchaseFeedbackService = purchaseFeedbackService;
    }


    /**
     * 添加采购反馈
     * @param list
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public R addPurchaseFeedback(List<PurchaseFeedback> list){
        Integer num=0;
        if (!CollectionUtils.isEmpty(list)){
            for (PurchaseFeedback p:list) {
                List<QueryPurchaseFeedbackVO> queryPFVOS=purchaseFeedbackService.getFeedbackDateByParam(p);
                if (!CollectionUtils.isEmpty(queryPFVOS)){
                    for (QueryPurchaseFeedbackVO q:queryPFVOS) {
                        p.setId(q.getId());
                        num+=purchaseFeedbackService.updateByPrimaryKeySelective(p);
                    }

                }else {
                    num+=purchaseFeedbackService.insertSelective(p);
                }
            }
        }

        if (num==list.size()){
            return R.buildError(ResultStatusEnum.SUCCESS);
        }else {
            return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
        }
    }
}
